#include "part_logic_handle.h"

static void print_data_hex(uint8_t *data, u16 len)
{   
    for (int i = 0; i < len; i++) 
    {
        printf(" %02x", data[i]);
    }
    
    printf(", len1: %d\n", len);
}


void mode_part_display(u8 len,u8 *RGB)
{
    u8 buf[BUF_SIZE];
    bzero(buf,BUF_SIZE);
    u8 part; //用于保存一段有多长
    part = get_light_length()/len;
    printf("part %d\n",part);
    //print_data_hex(RGB,len*3);
    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < part; j++)
        {
            buf[i*part*3+j*3+0] = RGB[i*3+0]; //R
            buf[i*part*3+j*3+1] = RGB[i*3+1]; //G
            buf[i*part*3+j*3+2] = RGB[i*3+2]; //B
        }
    }
    //print_data_hex(buf,get_color_length());
    logic_send_light_msg(buf,get_color_length());
}


void part_loigc_handle(SYS_OPT_KEY opt)
{
    static int time;
    if (app_logic_get_last_ssp() != app_logic_get_ssp()) //第一次进入
    {
        LOG_INFO("mode_part_11\n");
        set_custom_recssp(0); //强制复位
        app_logic_set_last_ssp(app_logic_get_ssp());
        time = arch_os_ms_now();
    }

    if (arch_os_ms_elapsed(time)>=(20)) //20ms进入一次
    {
        PART_INFO_T tmep_part_info; //暂时存储 分段信息
        light_get_part_info(&tmep_part_info);
        mode_part_display(tmep_part_info.len,tmep_part_info.RGB);
        time = arch_os_ms_now();
    }

    if (opt.oepation == OPT_GO_NEXT) //下一个
    {
        app_logic_set_ssp(SSP_MODE_SCENE);
    }
    else if(opt.oepation == OPT_GO_MIC) //进入麦克风模式
    {
        app_logic_set_ssp(SSP_MODE_MIC);
    }
    else if(opt.oepation == OPT_POWER_TURNOFF) //关机
    {
        app_logic_set_ssp(SSP_POWER_OFF);
    }

}